package com.example.zhu95.mytimber.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.adapters.ItemVpopupAdapter;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.ui.SongsSectionTitleIndicator;
import com.example.zhu95.mytimber.utils.MyComparator;

import java.util.Collections;
import java.util.List;

import xyz.danoz.recyclerviewfastscroller.vertical.VerticalRecyclerViewFastScroller;

public class SongsFragment extends BaseFragment {
    RecyclerView recyclerViewSongs;
    VerticalRecyclerViewFastScroller scroller;
    ItemVpopupAdapter adapter;
    LinearLayoutManager manager;
    SongsSectionTitleIndicator indicator;
    List<SongBean> songs = SongData.songs;

    public List<SongBean> getSongs() {
        return songs;
    }

    public void setSongs(List<SongBean> songs) {
        this.songs = songs;
        notifyDataSetChanged();
    }

    public ItemVpopupAdapter getAdapter() {
        return adapter;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_library_songs, container, false);
        recyclerViewSongs = view.findViewById(R.id.recycler_view_songs);
        scroller = view.findViewById(R.id.scroller_songs);
        indicator = view.findViewById(R.id.indicator_songs);
        // Set the adapter
        Context context = view.getContext();
        manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        if (adapter == null) {
            adapter = new ItemVpopupAdapter(songs, rootActivity);
        }

        recyclerViewSongs.setLayoutManager(manager);
        recyclerViewSongs.setAdapter(adapter);
        scroller.setRecyclerView(recyclerViewSongs);
        recyclerViewSongs.setOnScrollListener(scroller.getOnScrollListener());
        scroller.setSectionIndicator(indicator);
        return view;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        try {
            switch (item.getItemId()) {
                case R.id.sort_az:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("title")));
                    break;
                case R.id.sort_za:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("title"), true));
                    break;
                case R.id.sort_year:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("year")));
                    break;
                case R.id.sort_artist:
                    Collections.sort(songs, new MyComparator(SongBean.class.getMethod("getArtist")));
                    break;
                case R.id.sort_album:
                    Collections.sort(songs, new MyComparator(SongBean.class.getMethod("getAlbum")));
                    break;
                case R.id.sort_duration:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("duration")));
                    break;
                default:
            }
            adapter.notifyDataSetChanged();
        } catch (NoSuchFieldException | NoSuchMethodException e) {
            e.printStackTrace();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        notifyDataSetChanged();
    }

    public void notifyDataSetChanged() {
        adapter.setSongs(songs);
        adapter.notifyDataSetChanged();
    }

}
